package mya.mes.mdc.queries.alarm;

import mya.common.jpa.context.ServiceContext;
import mya.mes.mdc.model.alarm.MdcAlarm;

import javax.persistence.LockModeType;
import java.util.List;

public class MdcAlarmQuery {

    public static List<MdcAlarm> queryByZoneId(ServiceContext context, Long zoneId) throws Exception {

        List<MdcAlarm> list = context.em.createNamedQuery("MdcAlarm.zoneId", MdcAlarm.class)
                .setParameter(1, zoneId)
                //   .setLockMode(LockModeType.PESSIMISTIC_WRITE)
                .getResultList();


        return list;
    }

    public static List<MdcAlarm> byAlarmTypeId(ServiceContext context, Long alarmTypeId) throws Exception {

        List<MdcAlarm> list = context.em.createNamedQuery("MdcAlarm.byAlarmTypeId", MdcAlarm.class)
                .setParameter(1, alarmTypeId)
                //   .setLockMode(LockModeType.PESSIMISTIC_WRITE)
                .getResultList();


        return list;
    }

    public static List<MdcAlarm> byMdcTag(ServiceContext context, Long zoneId, String mdcTag) throws Exception {

        List<MdcAlarm> list = context.em.createNamedQuery("MdcAlarm.byMdcTag", MdcAlarm.class)
                .setParameter(1, zoneId)
                .setParameter(2, mdcTag)
                //   .setLockMode(LockModeType.PESSIMISTIC_WRITE)
                .getResultList();


        return list;
    }


    public static List<MdcAlarm> loadAll(ServiceContext context) throws Exception {

        List<MdcAlarm> list = context.em.createNamedQuery("MdcAlarm.loadAll", MdcAlarm.class)
                .getResultList();
        return list;
    }


    public static List<MdcAlarm> loadNeedDispatch(ServiceContext context, long currentTime) throws Exception {

        List<MdcAlarm> list = context.em.createNamedQuery("MdcAlarm.loadNeedDispatch", MdcAlarm.class)
                .setParameter(1, currentTime)
                .setParameter(2, currentTime)
                .setLockMode(LockModeType.PESSIMISTIC_WRITE)
                .getResultList();

        return list;
    }


}
